$config.wxVerify();

$.fn.extend({
    animateCss: function (animationName,fuc) {
        var animationEnd = 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend';
        this.addClass('animated ' + animationName).one(animationEnd, function() {
            $(this).removeClass('animated ' + animationName);
            if(fuc) fuc();
        });
    }
});

$(function () {
    $config.wxShare({
        title : '一句口号=五千元大奖！广州治水口号，你说了算！',
        cont : '广州治水宣传语大征集！',
        logo : 'http://h5.yixiaoqun.com/static/src/img/slogan/logo.jpg'
    })
    //显示说明内容
    $('.explain .say').on('click',function () {
        $('.box-explain').fadeIn(300);
    });

    $('.close').on('click',function () {
        $(this).closest('.box').fadeOut(300);
    })

    $('.toPage').on('click',function () {
        var page = $(this).data('page');
        $('.'+page).fadeIn(300);
    })
})

var app = new Vue({
    el: '#form',
    data: {
        lock : true,
        user:{
            name : '',
            mobile : '',
            details : [{
                slogan : '',
                explain : ''
            }]
        }
    },
    methods : {
        addItem : function () {
            this.user.details.push({
                slogan : '',
                explain : ''
            })
        },
        deteleItem : function (index) {
            this.user.details.splice(index,1);
        },
        submit : function () {
            // 校验
            if (!this.user.name) {
                swal({title: "请填写姓名!", type: "warning",});
                return;
            }
            if (!this.user.mobile) {
                swal({title: "请填写联系号码!", type: "warning",});
                return;
            }
            if (!(/^1[34578]\d{9}$/.test(this.user.mobile))) {
                swal({title: "手机号码格式有误!", type: "warning",});
                return;
            }
            var boole = true;
            for (var i = 0; i < this.user.details.length; i++) {
                if (!this.user.details[i].slogan || !this.user.details[i].explain) {
                    boole = false;
                }
            }
            if (!boole) {
                swal({title: "请填写口号和描述!", type: "warning",});
                return;
            }else{
                var details = [];
                for (var i = 0; i < this.user.details.length; i++){
                    var content = '口号：'+this.user.details[i].slogan +'\n'+'创作理念：'+this.user.details[i].explain;
                    details.push({
                        content : content
                    })
                }
            }

            if(!this.lock) return;
            this.lock = false;

            // 提交
            var _this = this,
                postData = {
                project: location.hostname == 'h5.yixiaoqun.com'?'gzzs':'test',
                realname: _this.user.name,
                mobile: _this.user.mobile,
                details: details
            }

            $.ajax({
                url : $config.host+'/H5/signUpDetail',
                type : 'post',
                dataType : "jsonp",
                jsonp : 'callback',
                data : postData,
                success : function (json) {
                    if(json.code == 1){
                        var href = $config.host+'/#/group/'+json.data.group_id;

                        $('.box-success').show();
                        $('.box-success .link a').attr('href',href);
                        var num = 3,
                            time = setInterval(function () {
                                num--;
                                if(num!=0){
                                    $('.box-success .link span').text(num);
                                }else{
                                    clearInterval(time);
                                    location.href = href;
                                }
                            },1000)

                    }else{
                        swal({title: json.message, type: "warning"});
                    }
                    setTimeout(function () {
                        _this.lock = true;
                    },1000)

                }
            })
        }
    }
})